
package at.fhooe.mcm12.java.clients;

import java.io.Serializable;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import at.fhooe.mcm12.java.interfaces.ISensorListener;
import at.fhooe.mcm12.java.interfaces.RMIClientManager;

/**
 * Base class for client implementations used with the ClientManager.
 * 
 * @author Patrick Hutflesz
 * 
 */
public abstract class AbstractClient implements ISensorListener, Serializable {

	protected RMIClientManager m_clientManager;
	
	protected AbstractClient(){
		initiateRMIClient();
	}
	/**
	 * @return the client's name
	 */
	public abstract String getName();
	
	@Override
	public String toString() {
		return getName();
	}
	
	private void initiateRMIClient() {
		System.setProperty("java.security.policy", "file:./res/rmi.policy");
		if (System.getSecurityManager() == null) {
			System.setSecurityManager(new SecurityManager());
		}

		try {
			Registry remoteRegistry = LocateRegistry.getRegistry(RMIClientManager.HOST, Registry.REGISTRY_PORT);
			m_clientManager = (RMIClientManager) remoteRegistry.lookup(RMIClientManager.NAME);
		} catch (RemoteException | NotBoundException e) {
			e.printStackTrace();
		}
	}
}
